package com.bluemsun.naturallanguageprocessing.service.impl;

import com.bluemsun.naturallanguageprocessing.common.ResponseCode;
import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.dao.ClassifyMapper;
import com.bluemsun.naturallanguageprocessing.dao.QuestionMapper;
import com.bluemsun.naturallanguageprocessing.entity.Classify;
import com.bluemsun.naturallanguageprocessing.service.IClassifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("classifyService")
public class ClassifyServiceImpl implements IClassifyService {

    @Resource
    private ClassifyMapper classifyMapper;
    @Resource
    private QuestionMapper questionMapper;

    @Override
    public ServerResponse<Object> getClassify(){
        List<Classify> classifyList = classifyMapper.selectAllClassify();
        if (classifyList.isEmpty()){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.DATASOURCE_ERROR.getCode(),ResponseCode.DATASOURCE_ERROR.getDesc());
        }else {
            return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(),classifyList);
        }
    }

    @Override
    public ServerResponse<Object> deleteClassify(Integer id){
        int resultCount = classifyMapper.deleteByPrimaryKey(id);
        int resultCount1 = questionMapper.deleteByClassifyId(id);
        if (resultCount == 0){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.PARAM_ERROR.getCode(), ResponseCode.PARAM_ERROR.getDesc());
        }else {
            return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc());
        }
    }

    @Override
    public ServerResponse<Object> insertClassify(Classify classify) {
        int resultCount = classifyMapper.insertSelective(classify);
        //返回值为1，表示增加成功，否则返回失败
        if (resultCount==1){
            return ServerResponse.createBySuccessMessage("添加类别成功");
        }else {
            return ServerResponse.createByErrorCodeMessage(-1,"数据库错误,添加类别失败");
        }
    }

    @Override
    public ServerResponse<Object> updateClassify(Classify classify) {
        int resultCount = classifyMapper.updateByPrimaryKeySelective(classify);
        //返回值为1，表示修改成功，否则返回失败
        if (resultCount==1){
            return ServerResponse.createBySuccessMessage("修改类别成功");
        }else if(resultCount==0){
            return ServerResponse.createByErrorCodeMessage(-1,"不存在该类别");
        }else {
            return ServerResponse.createByErrorCodeMessage(-1,"数据库错误,修改类别失败");
        }
    }
}
